package th.co.exat.uap.controller;

import com.csvreader.CsvWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import th.co.exat.uap.model.UAPData;

public class ExportRPT001 extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd;
        try {
            String uapDate = request.getParameter("uapDate");
            String toUapDate = request.getParameter("toUapDate");
            String plateNo = new String(request.getParameter("plate").getBytes("iso-8859-1"), "utf-8");
            String exitStation = request.getParameter("station2");
            String exp = request.getParameter("exp");

            if (exp.equals("Y")) {
                Format formatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
                String s = formatter.format(new Date());
                String file = "ใบทวงหนี้ทหาร-" + s + ".csv";

                String outputFile = request.getRealPath("//") + "/" + file;

                UAPData u = new UAPData();
                u.setUapDate(uapDate);
                u.setToUapDate(toUapDate);
                u.setPlateNo(plateNo);
                u.setUapExitStation(exitStation);

                List<UAPData> dataList = UAPData.getDataList(u, "P1");

                boolean alreadyExists = new File(outputFile).exists();
                CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, false), ',');

                if (!alreadyExists) {
                    csvOutput.write("ลำดับ");
                    csvOutput.write("เลขที่");
                    csvOutput.write("วันเวลา");
                    csvOutput.write("ด่าน");
                    csvOutput.write("ทะเบียน");
                    csvOutput.write("จังหวัด");
                    csvOutput.write("อัตรา");
                    csvOutput.write("ชำระเงิน");
                    csvOutput.write("ชื่อผู้ถือครอง");
                    csvOutput.write("ที่อยู่");
                    csvOutput.endRecord();
                }
                for (int i = 0; i < dataList.size(); i++) {
                    csvOutput.write(String.valueOf(i+1));
                    csvOutput.write(dataList.get(i).getDocumentNo());
                    csvOutput.write(dataList.get(i).getUapDate());
                    csvOutput.write(dataList.get(i).getUapExitStationName());
                    csvOutput.write(dataList.get(i).getPlateNo());
                    csvOutput.write(dataList.get(i).getProvinceName());
                    csvOutput.write(String.valueOf(dataList.get(i).getRate()));
                    csvOutput.write(String.valueOf(dataList.get(i).getPay()));
                    csvOutput.write(dataList.get(i).getOwnerPrefix()+
                                    dataList.get(i).getOwnerFname()+ " " +
                                    dataList.get(i).getOwnerLname());
                    csvOutput.write(dataList.get(i).getOwnerAddress() + " " +
                                    dataList.get(i).getOwnerDistric1() + " " +
                                    dataList.get(i).getOwnerDistric2() + " " +
                                    dataList.get(i).getOwnerProvince());
                    csvOutput.endRecord();
                }
                csvOutput.close();
                request.setAttribute("file", file);
            }

            rd = request.getRequestDispatcher("rpt001export.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
